package com.cskaoyan.service.promoteservice;

import com.cskaoyan.bean.promotebean.Coupon;
import com.cskaoyan.bean.promotebean.CouponExample;

import com.cskaoyan.bean.promotebean.CouponUser;
import com.cskaoyan.bean.promotebean.CouponUserExample;
import com.cskaoyan.bo.promoteBO.CouponCreateBO;
import com.cskaoyan.bo.promoteBO.CouponUpdateBO;
import com.cskaoyan.mapper.promotemapper.CouponMapper;
import com.cskaoyan.mapper.promotemapper.CouponUserMapper;
import com.cskaoyan.service.wx.WxCouponService;
import com.cskaoyan.vo.promotevo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: feisl
 * @Date: 2021/5/10 1:15
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    CouponMapper couponMapper;

    @Override
    public CouponListVO queryCouponAll(Integer page, Integer limit, String name, Short type, Short status, String sort, String order) {
        PageHelper.startPage(page, limit);
        CouponExample couponExample = new CouponExample();
        //条件查询
        CouponExample.Criteria criteria = couponExample.createCriteria();
//        if(name != null)criteria.andNameLike("%" + name + "%");
//        if(content != null)criteria.andContentLike("%" + content + "%");
//        criteria.andDeletedEqualTo(false);
        if (name != null) criteria.andNameLike("%" + name + "%");
        if (type != null) criteria.andTypeEqualTo(type);
        if (status != null) criteria.andStatusEqualTo(status);
        criteria.andDeletedEqualTo(false);


        couponExample.setOrderByClause(sort + " " + order);
        List<Coupon> couponList = couponMapper.selectByExample(couponExample);

        PageInfo<Coupon> couponPageInfo = new PageInfo<>(couponList);
        long total = couponPageInfo.getSize();
        CouponListVO couponListVO = new CouponListVO();
        couponListVO.setItems(couponList);
        couponListVO.setTotal(total);
        return couponListVO;
    }

    @Override
    public CouponCreateVO insert(CouponCreateBO c) throws ParseException {
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (c.getAddTime() == null) {
            c.setAddTime(date);
        }
        String format = simpleDateFormat.format(c.getAddTime());
        c.setUpdateTime(date);
        String format1 = simpleDateFormat.format(c.getUpdateTime());
        //开始时间为空，则创建开始和结束时间
        if (c.getStartTime() == null) {
            c.setStartTime(simpleDateFormat.format(date));
            Calendar calTemp = Calendar.getInstance();
            calTemp.add(Calendar.DATE, c.getDays());
            c.setEndTime(simpleDateFormat.format(calTemp.getTime()));
        }
        //持续时间为空则计算持续天数
        if (c.getDays() == 0) {
            Date start = simpleDateFormat.parse(c.getStartTime());
            Date end = simpleDateFormat.parse(c.getEndTime());
            short day = (short) ((end.getTime() - start.getTime()) / 86400 / 1000);
            c.setDays(day);
        }


        couponMapper.insertSelectiveBO(c);
        CouponCreateVO couponCreateVO = new CouponCreateVO(
                c.getId(), c.getName(), c.getDesc(), c.getTag(), c.getTotal(), c.getDiscount(),
                c.getMin(), c.getLimit(), c.getType(), c.getStatus(), c.getGoodsType(), c.getTimeType(), c.getDays(),
                format, format1, c.getGoodsValue()
        );
//        couponCreateVO.setId(c.getId());
//        couponCreateVO.setName(c.getName());
//        couponCreateVO.setDesc(c.getDesc());
//        couponCreateVO.setTag(c.getTag());
//        couponCreateVO.setTotal(c.getTotal());
//        couponCreateVO.setDiscount(c.getDiscount());
//        couponCreateVO.setMin(c.getMin());
//        couponCreateVO.setLimit(c.getLimit());
//        couponCreateVO.setType(c.getType());
//        couponCreateVO.setStatus(c.getStatus());
//        couponCreateVO.setGoodsType(c.getGoodsType());
//        couponCreateVO.setTimeType(c.getTimeType());
//        couponCreateVO.setDays(c.getDays());
//        couponCreateVO.setAddTime(format);
//        couponCreateVO.setUpdateTime(format1);
//        couponCreateVO.setGoodsValue(c.getGoodsValue());

        return couponCreateVO;
    }

    @Override
    public CouponUpdateVO update(CouponUpdateBO bo) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获得原始数据库时间信息，看是哪里发生了变化
        Coupon couponOrigin = couponMapper.selectByPrimaryKey(bo.getId());
        if(couponOrigin.getDays() == bo.getDays()){
            //发生变化的是开始时间和结束时间
            Date start = simpleDateFormat.parse(bo.getStartTime());
            Date end = simpleDateFormat.parse(bo.getEndTime());
            short day = (short) ((end.getTime() - start.getTime()) / 86400 / 1000);
            bo.setDays(day);
        }
        else{
            //发生变化的是days
            short day = bo.getDays();
            Date start = simpleDateFormat.parse(bo.getStartTime());
            Calendar calendar = Calendar.getInstance();
            long time = (start.getTime() - calendar.getTime().getTime())/1000/86400 + day;//时间为天
            calendar.add(Calendar.DATE,(int)time);
            bo.setEndTime(simpleDateFormat.format(calendar.getTime()));
        }

        Coupon coupon = new Coupon(
                bo.getId(), bo.getName(), bo.getDesc(), bo.getTag(), bo.getTotal(), bo.getDiscount(), bo.getMin(),
                bo.getLimit(), bo.getType(), bo.getStatus(), bo.getGoodsType(), bo.getGoodsValue(), bo.getCode(),
                bo.getTimeType(), bo.getDays(), simpleDateFormat.parse(bo.getStartTime()), simpleDateFormat.parse(bo.getEndTime()),
                simpleDateFormat.parse(bo.getAddTime()), new Date(), bo.getDeleted()
        );


        String addTime = simpleDateFormat.format(coupon.getAddTime());
        String updateTime = simpleDateFormat.format(coupon.getUpdateTime());
        String endTime = simpleDateFormat.format(coupon.getEndTime());
        String startTime = simpleDateFormat.format(coupon.getStartTime());
        couponMapper.updateByPrimaryKey(coupon);

        CouponUpdateVO couponUpdateVO = new CouponUpdateVO(
                coupon.getId(), coupon.getName(), coupon.getDesc(), coupon.getTag(), coupon.getTotal(), coupon.getDiscount(),
                coupon.getMin(), coupon.getLimit(), coupon.getType(), coupon.getStatus(), coupon.getGoodsType(), coupon.getTimeType(),
                coupon.getDays(), startTime, endTime, addTime, updateTime, coupon.getDeleted(), coupon.getGoodsValue()
        );

        return couponUpdateVO;
    }

    /*
    给的数据库coupon表里缺少startTime和endTime，演示会出问题，在create接口实现了自动填补day或start end
     */
    @Override
    public CouponReadVO read(Integer id) {
        Coupon coupon = couponMapper.selectByPrimaryKey(id);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String addTime = simpleDateFormat.format(coupon.getAddTime());
        String updateTime = simpleDateFormat.format(coupon.getUpdateTime());
        String endTime = simpleDateFormat.format(coupon.getEndTime());
        String startTime = simpleDateFormat.format(coupon.getStartTime());


        CouponReadVO couponReadVO = new CouponReadVO(
                coupon.getId(), coupon.getName(), coupon.getDesc(), coupon.getTag(), coupon.getTotal(),
                coupon.getDiscount(), coupon.getMin(), coupon.getLimit(), coupon.getType(), coupon.getStatus(),
                coupon.getGoodsType(), coupon.getTimeType(), coupon.getDays(), startTime, endTime, addTime, updateTime,
                coupon.getDeleted(), coupon.getGoodsValue()
        );
        return couponReadVO;
    }

    @Autowired
    WxCouponService wxCouponService;

    @Override
    public void delete(Integer id) {
//        couponMapper.deleteByPrimaryKey(id);
        wxCouponService.delete(id);


    }

    @Autowired
    CouponUserMapper couponUserMapper;
    @Override
    public ListuserVO listuser(Integer page, Integer limit, Integer couponId, Integer userId, Short status, String sort, String order) {
        PageHelper.startPage(page,limit);
        CouponUserExample couponUserExample = new CouponUserExample();
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        criteria.andCouponIdEqualTo(couponId);
        if(userId != null)criteria.andUserIdEqualTo(userId);
        if(status != null)criteria.andStatusEqualTo(status);
        criteria.andDeletedEqualTo(false);

        couponUserExample.setOrderByClause(sort + " " + order);
        List<CouponUser> couponUserList = couponUserMapper.selectByExample(couponUserExample);

        PageInfo<CouponUser> couponUserPageInfo = new PageInfo<>(couponUserList);
       long total = couponUserPageInfo.getSize();
        ListuserVO listuserVO = new ListuserVO();
        listuserVO.setTotal(total);
        listuserVO.setItems(couponUserList);
        return listuserVO;

    }


}
